<?php
require_once 'EZend/Controller/AdminBaseController.php';

class Admin_CategoryController extends AdminBaseController {
	public function preDispatch() {
		parent::preDispatch();
		$this->_helper->layout->setLayout('iframe');
	}
	
	public function indexAction() {
		$this->view->assign('groups', $this->getCmsArticleService()->listAllCategories());
	}
	
	public function addAction() {
		$parentCategoryId = intval($this->getRequest()->getParam('parent_id'));
		if ($parentCategoryId) $parentCategory = $this->getCmsArticleService()->getCategory($parentCategoryId);
		
		if ($parentCategoryId && !$parentCategory) return $this->showNotice('找不到父类');
		
		$this->view->assign('parentCategory', $parentCategoryId ? $parentCategory : array('id' => 0, 'title' => '根分类'));
	}
	
	public function doaddAction() {
		if (!$this->getRequest()->isPost()) return $this->showNotice('非法请求');
		
		$fields = array(
			'title' => trim($this->getRequest()->getPost('title')),
			'name' => trim($this->getRequest()->getPost('name')),
			'parent_id' => intval($this->getRequest()->getPost('parent_id')),
			'order_number' => intval($this->getRequest()->getPost('order_number')),
			'description' => trim($this->getRequest()->getPost('description')),
		);
		if ('' == $fields['title'] || '' == $fields['name']) return $this->showNotice('标题和名称必填');
		
		if (!$this->getCmsArticleService()->addCategory($fields)) return $this->showNotice('添加失败，请重试');
		return $this->showNotice('添加成功', $this->getInnerUrl());
	}
	
	public function editAction() {
		$id = (int) $this->getRequest()->getParam('id');
		if ($id <= 0) return $this->showNotice('参数错误，请重试');
		
		$category = $this->getCmsArticleService()->getCategory($id);
		if (!$category) return $this->showNotice('找不到分类');
		
		$parentCategory = $category['parent_id'] ? $this->getCmsArticleService()->getCategory($category['parent_id']) : array('id' => 0, 'title' => '根分类');
		
		$this->view->assign('parentCategory', $parentCategory);
		$this->view->assign('category', $category);
	}
	
	public function doeditAction() {
		if (!$this->getRequest()->isPost()) return $this->showNotice('非法请求');
		$id = (int) $this->getRequest()->getParam('id');
		if ($id <= 0) return $this->showNotice('参数错误，请重试');
		
		$fields = array(
			'title' => trim($this->getRequest()->getPost('title')),
			'name' => trim($this->getRequest()->getPost('name')),
			'order_number' => intval($this->getRequest()->getPost('order_number')),
			'description' => trim($this->getRequest()->getPost('description')),
		);
		if ('' == $fields['title'] || '' == $fields['name']) return $this->showNotice('标题和名称必填');
		
		if (!$this->getCmsArticleService()->editCategory($id, $fields)) return $this->showNotice('所有信息均未修改');
		return $this->showNotice('编辑成功', $this->getInnerUrl());
	}
	
	public function deleteAction() {
		$id = (int) $this->getRequest()->getParam('id');
		if ($id <= 0) return $this->showNotice('参数错误，请重试');
		
		$category = $this->getCmsArticleService()->getCategory($id);
		if (!$category) return $this->showNotice('找不到分类');
		
		if ($this->getCmsArticleService()->listSubCategories($id)) return $this->showNotice('有子类，要先删除');
		
		if (!$this->getCmsArticleService()->deleteCategory($id)) return $this->showNotice('删除失败，请重试');
		return $this->showNotice('删除成功', $this->getInnerUrl());
	}
	
	private function getCmsArticleService() {
		require_once 'Models/Cms/Service/CmsServiceFactory.php';
		return CmsServiceFactory::getInstance()->createCmsArticleService();
	}
}